﻿using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;

namespace AiQiuQuan.Sport.Core
{
    /// <summary>
    /// EntityFrameworkCoreExtensions
    /// </summary>
    public static class EntityFrameworkCoreExtensions
    {
        /// <summary>
        /// mysql dbcontext
        /// </summary>
        /// <typeparam name="TDbContext">DbContext</typeparam>
        /// <param name="services">ServiceCollection</param>
        /// <param name="connectionStr">数据库连接字符串</param>
        /// <param name="serviceLifetime">dbcontext服务生命周期</param>
        /// <returns></returns>
        public static IServiceCollection AddMsSqlDbContext<TDbContext>(this IServiceCollection services, string connectionStr, ServiceLifetime serviceLifetime = ServiceLifetime.Scoped) where TDbContext : DbContext
        {
            services.AddScoped<ISplitTableInfo, BaseSplitTableInfo>();
            services.AddDbContext<TDbContext>(optionsBuilder =>
            {
                optionsBuilder.EnableSensitiveDataLogging();
                optionsBuilder.UseSqlServer(connectionStr, Option =>
                {
                });
                optionsBuilder.UseBatchEF_MSSQL();
            }, serviceLifetime, serviceLifetime);
            services.AddScoped<IUnitOfWorkManager, UnitOfWorkManager<TDbContext>>();

            return services;
        }
    }
}
